package daniel.kisti.Helper;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.os.Environment;
import android.util.Log;
//import android.util.Log;
//import daniel.kisti.serverModule.RequestDataToTierServer;

/**
 * <pre>
 * daniel.kisti.Helper
 *   |_ Caf_Helper.java
 * 
 * </pre>
 * 
 * Desc : CDF 클러스터의 시스템정보, JOB 실행상황, 워커노드들의 실행 정보를 TierServer로 부터 가져온다.
 * 
 * @Company : KISTI
 * @Author :daniel
 * @Date :2011. 10. 26. 오후 5:29:56
 * @Version: V1
 * 
 */
public class Caf_Helper {

//	private RequestDataToTierServer requestDataToTierServer;
	private String[][] workernodeStatus;
	private String[][] cdfJobs;
	private String[] queueStatus;

	/**
	 * Desc : Constructor of Caf_Helper.java class
	 */
	public Caf_Helper() {
		// TODO Auto-generated constructor stub
//		requestDataToTierServer = new RequestDataToTierServer();
	}

	/**
	 * 
	 * Desc : CDF 클러스터의 Job 수행상태의 그래프를 이미지 형식으로 다운로드 한다. 이 이미지를 가지고있는
	 * TierServer의 이미지 주소를 가져온다.
	 * 
	 * @Method Name : getCeMonURL
	 * @return 다운받을 TierServer의 Graph 이미지 주소 반환
	 * 
	 */
	private String getCeMonURL() {
		String URL = "";
		try {

//			URL = (String) requestDataToTierServer.request("URL_requestURL");
		} catch (Exception e) {
			// TODO: handle exception
//			Log.d("[Caf_Helper]", e.toString());
		}

		return URL;
	}

	/**
	 * 
	 * Desc : CDF 클러스터의 Job 수행상태의 그래프를 이미지 형식으로 TierServer로부터 다운로드 받아 External
	 * Disk에 저장한다.
	 * 
	 * @Method Name : getCeMonGraphImage
	 * @return 사용가능한 graphImage path 를 반환
	 * 
	 */
	public String getCeMonGraphImage() {
		String imgPath = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		imgPath += "/ce02Graph.bmp";
		// requestDataToTierServer.requestImgData("URL_requestURL");

		return imgPath;
	}

	/**
	 * 
	 * Desc : CDF 클러스터에서 수행되는 JOB Queue 정보를 TierServer 로부터 가져온다.
	 * 
	 * @Method Name : getQueueStatus
	 * @return CDF JOB Queue 상태를 반환
	 * 
	 */
	public String[] getQueueStatus() {
		// String queueStatus =
		// (String)requestDataToTierServer.request("QueueStatus");
		String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Data><Node><Queue>osgcdf</Queue><Total>70</Total><Run>60</Run><Held>5</Held><Wait>5</Wait></Node></Data>";

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream istream = new ByteArrayInputStream(
					result.getBytes("utf-8"));
			Document doc = builder.parse(istream);

			Element order = doc.getDocumentElement();
			NodeList items = order.getElementsByTagName("Node");

			queueStatus = new String[5];

//			for (int i = 0; i < items.getLength(); i++) {
				Element element = (Element) items.item(0);

	
				queueStatus[0] = getChildren(element, "Queue");
				queueStatus[1] = getChildren(element, "Total");
				queueStatus[2] = getChildren(element, "Run");
				queueStatus[3] = getChildren(element, "Held");
				queueStatus[4] = getChildren(element, "Wait");
//			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return queueStatus;
	}

	/**
	 * 
	 * Desc : CDF 클러스터의 워커노드들의 상태를 TierServer 로부터 가져온다.
	 * 
	 * @Method Name : getWorkernodeStatus
	 * @return [name, state, number of processor, jobs ] 을 배열에 순차적으로 담아서 반환
	 * 
	 */
	public String[][] getWorkernodeStatus() {
		// String wkStatus =
		// (String)requestDataToTierServer.request("WorkerNodeStatus");
		String wkStatus = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Data><Node><name>wn2001</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2001.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704076,totmem=26756332kb,availmem=26056156kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=657191028199,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2002</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2002.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704082,totmem=26756332kb,availmem=26054092kb,physmem=24659188kb,ncpus=24,loadave=0.06,gres=,netload=696960893149,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2003</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2003.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704086,totmem=26756332kb,availmem=26049084kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=752220548587,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2004</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2004.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704056,totmem=26756332kb,availmem=26059496kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=790381084490,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2005</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2005.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704063,totmem=26756332kb,availmem=26033100kb,physmem=24659188kb,ncpus=24,loadave=0.04,gres=,netload=823782150033,state=free,jobs=,varattr=,rectime=1321856807</status></Node><Node><name>wn2006</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2006.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704054,totmem=26756332kb,availmem=26045436kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=845850652523,state=free,jobs=,varattr=,rectime=1321856806</status></Node><Node><name>wn2007</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2007.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704063,totmem=26756332kb,availmem=26042776kb,physmem=24659188kb,ncpus=24,loadave=0.09,gres=,netload=901575885673,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2008</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2008.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704058,totmem=26756332kb,availmem=26055176kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=893723555754,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2009</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2009.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704080,totmem=26756332kb,availmem=26054040kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=880940087570,state=free,jobs=,varattr=,rectime=1321856814</status></Node><Node><name>wn2010</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2010.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704049,totmem=26756332kb,availmem=26018428kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=906655727329,state=free,jobs=,varattr=,rectime=1321856811</status></Node><Node><name>wn2011</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2011.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704057,totmem=26756332kb,availmem=26062908kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=941525448609,state=free,jobs=,varattr=,rectime=1321856814</status></Node><Node><name>wn2012</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2012.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704082,totmem=26756332kb,availmem=26046468kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1008666566613,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2013</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2013.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704137,totmem=26756332kb,availmem=25768612kb,physmem=24659188kb,ncpus=12,loadave=0.00,gres=,netload=946327531203,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2014</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2014.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704059,totmem=26756332kb,availmem=25752016kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=982649988222,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2015</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2015.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704054,totmem=26756332kb,availmem=25712876kb,physmem=24659188kb,ncpus=24,loadave=0.02,gres=,netload=958716788154,state=free,jobs=,varattr=,rectime=1321856808</status></Node><Node><name>wn2016</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2016.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704058,totmem=26756332kb,availmem=25747272kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1016643935359,state=free,jobs=,varattr=,rectime=1321856810</status></Node><Node><name>wn2017</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2017.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=3903049,totmem=26756332kb,availmem=25714292kb,physmem=24659188kb,ncpus=24,loadave=0.06,gres=,netload=1032065479694,state=free,jobs=,varattr=,rectime=1321856814</status></Node><Node><name>wn2018</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2018.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704111,totmem=26756332kb,availmem=25684544kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1041307508382,state=free,jobs=,varattr=,rectime=1321856810</status></Node><Node><name>wn2019</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2019.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704078,totmem=26756332kb,availmem=25712732kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1128308787294,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2020</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2020.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704061,totmem=26756332kb,availmem=25778600kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1138271846861,state=free,jobs=,varattr=,rectime=1321856814</status></Node><Node><name>wn2021</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2021.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704071,totmem=26756332kb,availmem=25750256kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1230858094438,state=free,jobs=,varattr=,rectime=1321856808</status></Node><Node><name>wn2022</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2022.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704068,totmem=26756332kb,availmem=25767520kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1224416554343,state=free,jobs=,varattr=,rectime=1321856815</status></Node><Node><name>wn2023</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2023.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4704051,totmem=26756332kb,availmem=25754504kb,physmem=24659188kb,ncpus=24,loadave=0.03,gres=,netload=1325208084274,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2024</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2024.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=26716,nsessions=1,nusers=1,idletime=4651366,totmem=26756332kb,availmem=25863232kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1366405664370,state=free,jobs=,varattr=,rectime=1321856812</status></Node><Node><name>wn2025</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2025.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4653102,totmem=26756332kb,availmem=26007640kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1441068506730,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2026</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2026.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4653068,totmem=26756332kb,availmem=26031696kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1474185798952,state=free,jobs=,varattr=,rectime=1321856813</status></Node><Node><name>wn2027</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2027.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4653070,totmem=26756332kb,availmem=26013036kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1535841371686,state=free,jobs=,varattr=,rectime=1321856815</status></Node><Node><name>wn2028</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2028.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4653073,totmem=26756332kb,availmem=25977948kb,physmem=24659188kb,ncpus=24,loadave=0.00,gres=,netload=1633965981721,state=free,jobs=,varattr=,rectime=1321856812</status></Node><Node><name>wn2029</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2029.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=4653047,totmem=26756332kb,availmem=26002296kb,physmem=24659188kb,ncpus=24,loadave=0.01,gres=,netload=1600109972821,state=free,jobs=,varattr=,rectime=1321856815</status></Node><Node><name>wn2030</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><status>opsys=linux,uname=Linux wn2030.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=? 0,nsessions=? 0,nusers=0,idletime=3023781,totmem=26756332kb,availmem=25996960kb,physmem=24659188kb,ncpus=24,loadave=0.01,gres=,netload=1647621265208,state=free,jobs=,varattr=,rectime=1321856812</status></Node><Node><name>wn2031</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>0/417573.ce03.sdfarm.kr, 1/417578.ce03.sdfarm.kr, 2/417582.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2031.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=24544 22000 19854,nsessions=3,nusers=1,idletime=4653068,totmem=26756332kb,availmem=25385428kb,physmem=24659188kb,ncpus=24,loadave=3.00,gres=,netload=1700656585045,state=free,jobs=417573.ce03.sdfarm.kr 417578.ce03.sdfarm.kr 417582.ce03.sdfarm.kr,varattr=,rectime=1321856809</status></Node><Node><name>wn2032</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>0/417563.ce03.sdfarm.kr, 1/417566.ce03.sdfarm.kr, 2/417572.ce03.sdfarm.kr, 3/417575.ce03.sdfarm.kr, 4/417577.ce03.sdfarm.kr, 5/417579.ce03.sdfarm.kr, 6/417580.ce03.sdfarm.kr, 7/417581.ce03.sdfarm.kr, 8/417583.ce03.sdfarm.kr, 9/417584.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2032.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=31332 1028 24968 3157 29182 27092 15943 18066 20207 22864,nsessions=10,nusers=1,idletime=3869545,totmem=26756332kb,availmem=23107220kb,physmem=24659188kb,ncpus=12,loadave=7.08,gres=,netload=1390934159370,state=free,jobs=417563.ce03.sdfarm.kr 417566.ce03.sdfarm.kr 417572.ce03.sdfarm.kr 417575.ce03.sdfarm.kr 417577.ce03.sdfarm.kr 417579.ce03.sdfarm.kr 417580.ce03.sdfarm.kr 417581.ce03.sdfarm.kr 417583.ce03.sdfarm.kr 417584.ce03.sdfarm.kr,varattr=,rectime=1321856807</status></Node><Node><name>wn2033</name><state>job-exclusive,busy</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>0/417557.ce03.sdfarm.kr, 1/417559.ce03.sdfarm.kr, 2/417562.ce03.sdfarm.kr, 3/417564.ce03.sdfarm.kr, 4/417565.ce03.sdfarm.kr, 5/417567.ce03.sdfarm.kr, 6/417568.ce03.sdfarm.kr, 7/417569.ce03.sdfarm.kr, 8/417570.ce03.sdfarm.kr, 9/417571.ce03.sdfarm.kr, 10/417574.ce03.sdfarm.kr, 11/417576.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2033.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=32699 24231 26343 28471 30591 4276 6392 9316 15809 17912 20002 22087,nsessions=12,nusers=1,idletime=3869574,totmem=26756332kb,availmem=23165148kb,physmem=24659188kb,ncpus=12,loadave=10.05,gres=,netload=1433560515839,state=busy,jobs=417557.ce03.sdfarm.kr 417559.ce03.sdfarm.kr 417562.ce03.sdfarm.kr 417564.ce03.sdfarm.kr 417565.ce03.sdfarm.kr 417567.ce03.sdfarm.kr 417568.ce03.sdfarm.kr 417569.ce03.sdfarm.kr 417570.ce03.sdfarm.kr 417571.ce03.sdfarm.kr 417574.ce03.sdfarm.kr 417576.ce03.sdfarm.kr,varattr=,rectime=1321856815</status></Node><Node><name>wn2034</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>1/417543.ce03.sdfarm.kr, 2/417547.ce03.sdfarm.kr, 5/417553.ce03.sdfarm.kr, 6/417554.ce03.sdfarm.kr, 7/417555.ce03.sdfarm.kr, 9/417558.ce03.sdfarm.kr, 10/417560.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2034.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=32750 30606 6749 10948 15716 8836 17807,nsessions=7,nusers=1,idletime=3869571,totmem=26756332kb,availmem=24593316kb,physmem=24659188kb,ncpus=12,loadave=5.29,gres=,netload=1464306521836,state=free,jobs=417543.ce03.sdfarm.kr 417547.ce03.sdfarm.kr 417553.ce03.sdfarm.kr 417554.ce03.sdfarm.kr 417555.ce03.sdfarm.kr 417558.ce03.sdfarm.kr 417560.ce03.sdfarm.kr,varattr=,rectime=1321856808</status></Node><Node><name>wn2035</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>0/417526.ce03.sdfarm.kr, 1/417530.ce03.sdfarm.kr, 2/417533.ce03.sdfarm.kr, 5/417542.ce03.sdfarm.kr, 6/417544.ce03.sdfarm.kr, 7/417545.ce03.sdfarm.kr, 8/417546.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2035.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=30905 550 26623 28724 15439 17557 19679,nsessions=7,nusers=1,idletime=3869593,totmem=26756332kb,availmem=24341148kb,physmem=24659188kb,ncpus=12,loadave=4.43,gres=,netload=1538073861113,state=free,jobs=417526.ce03.sdfarm.kr 417530.ce03.sdfarm.kr 417533.ce03.sdfarm.kr 417542.ce03.sdfarm.kr 417544.ce03.sdfarm.kr 417545.ce03.sdfarm.kr 417546.ce03.sdfarm.kr,varattr=,rectime=1321856815</status></Node><Node><name>wn2036</name><state>free</state><np>12</np><properties>osgcdf</properties><ntype>cluster</ntype><jobs>0/417524.ce03.sdfarm.kr, 1/417525.ce03.sdfarm.kr, 2/417527.ce03.sdfarm.kr, 6/417532.ce03.sdfarm.kr, 8/417535.ce03.sdfarm.kr, 9/417536.ce03.sdfarm.kr, 10/417537.ce03.sdfarm.kr</jobs><status>opsys=linux,uname=Linux wn2036.sdfarm.kr 2.6.18-274.3.1.el5 #1 SMP Tue Sep 6 18:52:56 EDT 2011 x86_64,sessions=30842 593 24537 28745 14068 11983 16161,nsessions=7,nusers=1,idletime=3869825,totmem=26756332kb,availmem=24782592kb,physmem=24659188kb,ncpus=24,loadave=5.59,gres=,netload=1684848563549,state=free,jobs=417524.ce03.sdfarm.kr 417525.ce03.sdfarm.kr 417527.ce03.sdfarm.kr 417532.ce03.sdfarm.kr 417535.ce03.sdfarm.kr 417536.ce03.sdfarm.kr 417537.ce03.sdfarm.kr,varattr=,rectime=1321856816</status></Node></Data>";
		/**
		 * XML 파싱
		 */

//		Log.d("[Caf_Helper]", "parsing....................");

		workernodeStatus = new String[36][6];

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream istream = new ByteArrayInputStream(
					wkStatus.getBytes("utf-8"));
			Document doc = builder.parse(istream);

//			Log.d("[Caf_Helper]", wkStatus);

			Element order = doc.getDocumentElement();
			NodeList items = order.getElementsByTagName("Node");

			for (int i = 0; i < items.getLength(); i++) {
				Element element = (Element) items.item(i);

				String name = getChildren(element, "name");
				String state = getChildren(element, "state");
				String np = getChildren(element, "np");
				String status = getChildren(element, "status");

				workernodeStatus[i][0] = name;
				workernodeStatus[i][1] = state;
				workernodeStatus[i][2] = np;

				int start, end;
				start = status.indexOf("jobs");
				end = status.indexOf("varattr");
				String jobs = status.substring(start + 5, end - 1);
				jobs = jobs.replace(" ", "\n");

				workernodeStatus[i][3] = jobs;
			}

		} catch (Exception e) {
			// TODO: handle exception
//			Log.d("[Caf_Helper]", e.toString());
			e.printStackTrace();
		}

		return workernodeStatus;
	}

	/**
	 * 
	 * Desc : CDF 주별 JOB 실행 정보를 TierServer로부터 가져온다.
	 * 
	 * @Method Name : requestJobStatus
	 * @return 주별 JOB정보를 배열에 담아서 반환 [ month, period start, period end, User,
	 *         sucessJob, ratioSucessJob, WallTime, status 정보 ]
	 * 
	 */
	private String[][] requestJobStatus() {

		// String result =
		// (String)requestDataToTierServer.request("DB_requestCdfJobTable");
		String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Data><Node><month>201101</month><period1>2011-01-01</period1><period2>2011-01-31</period2><user>5</user><jobTotal>34037</jobTotal><sucessJob>33058</sucessJob><ratioSucessJob>97.1</ratioSucessJob><totalWallTime>73669</totalWallTime><successWallTime>68032</successWallTime><ratioSucessWallTime>92.3</ratioSucessWallTime><status>Online</status></Node><Node><month>201102</month><period1>2011-02-01</period1><period2>2011-02-28</period2><user>2</user><jobTotal>20183</jobTotal><sucessJob>20183</sucessJob><ratioSucessJob>100</ratioSucessJob><totalWallTime>91541</totalWallTime><successWallTime>91541</successWallTime><ratioSucessWallTime>100</ratioSucessWallTime><status>Online</status></Node><Node><month>201103</month><period1>2011-03-01</period1><period2>2011-03-31</period2><user>2</user><jobTotal>30527</jobTotal><sucessJob>30459</sucessJob><ratioSucessJob>99.8</ratioSucessJob><totalWallTime>135688</totalWallTime><successWallTime>135540</successWallTime><ratioSucessWallTime>99.9</ratioSucessWallTime><status>Online</status></Node><Node><month>201104</month><period1>2011-04-01</period1><period2>2011-04-30</period2><user>5</user><jobTotal>41974</jobTotal><sucessJob>41972</sucessJob><ratioSucessJob>100</ratioSucessJob><totalWallTime>182937</totalWallTime><successWallTime>182937</successWallTime><ratioSucessWallTime>100</ratioSucessWallTime><status>Online</status></Node><Node><month>201105</month><period1>2011-05-01</period1><period2>2011-05-31</period2><user>5</user><jobTotal>26447</jobTotal><sucessJob>26029</sucessJob><ratioSucessJob>98.4</ratioSucessJob><totalWallTime>91031</totalWallTime><successWallTime>90609</successWallTime><ratioSucessWallTime>99.5</ratioSucessWallTime><status>Online</status></Node><Node><month>201106</month><period1>2011-06-01</period1><period2>2011-06-30</period2><user>2</user><jobTotal>18589</jobTotal><sucessJob>18235</sucessJob><ratioSucessJob>98.1</ratioSucessJob><totalWallTime>164345</totalWallTime><successWallTime>163380</successWallTime><ratioSucessWallTime>99.4</ratioSucessWallTime><status>Online</status></Node><Node><month>201107</month><period1>2011-07-01</period1><period2>2011-07-31</period2><user>2</user><jobTotal>34291</jobTotal><sucessJob>34291</sucessJob><ratioSucessJob>99.6</ratioSucessJob><totalWallTime>209342</totalWallTime><successWallTime>204903</successWallTime><ratioSucessWallTime>97.9</ratioSucessWallTime><status>Online</status></Node><Node><month>201108</month><period1>2011-08-01</period1><period2>2011-08-31</period2><user>3</user><jobTotal>18197</jobTotal><sucessJob>15421</sucessJob><ratioSucessJob>84.7</ratioSucessJob><totalWallTime>58713</totalWallTime><successWallTime>58616</successWallTime><ratioSucessWallTime>99.8</ratioSucessWallTime><status>Online</status></Node><Node><month>201109</month><period1>2011-09-01</period1><period2>2011-09-30</period2><user>4</user><jobTotal>34974</jobTotal><sucessJob>34974</sucessJob><ratioSucessJob>45.1</ratioSucessJob><totalWallTime>47902</totalWallTime><successWallTime>47206</successWallTime><ratioSucessWallTime>98.5</ratioSucessWallTime><status>Online</status></Node><Node><month>201110</month><period1>2011-10-01</period1><period2>2011-10-31</period2><user>2</user><jobTotal>29016</jobTotal><sucessJob>29016</sucessJob><ratioSucessJob>100</ratioSucessJob><totalWallTime>95977</totalWallTime><successWallTime>95977</successWallTime><ratioSucessWallTime>100</ratioSucessWallTime><status>Online</status></Node><Node><month>201111</month><period1>2011-11-01</period1><period2>2011-11-30</period2><user>2</user><jobTotal>18183</jobTotal><sucessJob>17279</sucessJob><ratioSucessJob>95</ratioSucessJob><totalWallTime>131900</totalWallTime><successWallTime>103427</successWallTime><ratioSucessWallTime>78.4</ratioSucessWallTime><status>Online</status></Node><Node><month>201112</month><period1>2011-12-01</period1><period2>2011-12-31</period2><user>2</user><jobTotal>27807</jobTotal><sucessJob>14082</sucessJob><ratioSucessJob>50.6</ratioSucessJob><totalWallTime>107060</totalWallTime><successWallTime>105269</successWallTime><ratioSucessWallTime>98.3</ratioSucessWallTime><status>Online</status></Node></Data>";
		// 파싱
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream istream = new ByteArrayInputStream(
					result.getBytes("utf-8"));
			Document doc = builder.parse(istream);

//			Log.d("[Caf_Helper]", result);

			Element order = doc.getDocumentElement();
			NodeList items = order.getElementsByTagName("Node");

			cdfJobs = new String[items.getLength()][10];

			for (int i = 0; i < items.getLength(); i++) {
				Element element = (Element) items.item(i);

				String month = getChildren(element, "month");
				String period = getChildren(element, "period1");
				period += "~";
				period += getChildren(element, "period2");
				String user = getChildren(element, "user");
				String jobTotal = getChildren(element, "jobTotal");
				String sucessJob = getChildren(element, "sucessJob");
				String ratioSucessJob = getChildren(element, "ratioSucessJob");
				String totalWallTime = getChildren(element, "totalWallTime");
				String successWallTime = getChildren(element, "successWallTime");
				String ratioSucessWallTime = getChildren(element,
						"ratioSucessWallTime");
				String status = getChildren(element, "status");

				cdfJobs[i][0] = month;
				cdfJobs[i][1] = period;
				cdfJobs[i][2] = user;
				cdfJobs[i][3] = jobTotal;
				cdfJobs[i][4] = sucessJob;
				cdfJobs[i][5] = ratioSucessJob;
				cdfJobs[i][6] = totalWallTime;
				cdfJobs[i][7] = successWallTime;
				cdfJobs[i][8] = ratioSucessWallTime;
				cdfJobs[i][9] = status;
				// cdfJobs[i][10] = status;

			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
//			Log.d("[Caf_Helper]", e.toString());
		}
		return cdfJobs;
	}

	/**
	 * 
	 * Desc : CDF 주별 JOB 실행 정보를 TierServer로부터 가져온다.
	 * 
	 * @Method Name : getJobStatus
	 * @return 주별 JOB정보를 배열에 담아서 반환 [ month, period start, period end, User,
	 *         sucessJob, ratioSucessJob, WallTime, status 정보 ]
	 * 
	 */
	public String[][] getJobStatus() {
		requestJobStatus();
		return cdfJobs;
	}

	/**
	 * 
	 * Desc : CDF JOB실행수를 월별 형태로 계산하여 반환
	 * 
	 * @Method Name : getJobGraphData
	 * @return 월별 CDF JOB 실행량을 정수배열 형태로 반환
	 * 
	 */
	public int[] getJobGraphData() {

		requestJobStatus();

		int[] jobGraphData = new int[36];
		for (int i = 0; i < jobGraphData.length; i++) {
			jobGraphData[i] = 0;
		}

		for (int i = 0; i < cdfJobs.length; i++) {			
			jobGraphData[i] = Integer.parseInt(cdfJobs[i][3]);
		}


		return jobGraphData;
	}

	/**
	 * 
	 * Desc : XML 데이터 파싱 / 해당 속성값 반환
	 * 
	 * @Method Name : getChildren
	 * @param element
	 * @param tagName
	 * @return tag에 의한 속성값 찾아서 반환
	 * 
	 */
	private static String getChildren(Element element, String tagName) {
		NodeList list = element.getElementsByTagName(tagName);
		Element cElement = (Element) list.item(0);

		if (cElement.getFirstChild() != null) {
			return cElement.getFirstChild().getNodeValue();
		} else {
			return "";
		}
	}

	/**
	 * Desc :
	 * 
	 * @Method Name : main
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Caf_Helper ch = new Caf_Helper();

	}
}
